package com.nny.alarm.more;

import java.text.DateFormatSymbols;
import java.util.Calendar;

import android.annotation.SuppressLint;
import android.content.Context;
import android.text.format.DateFormat;

import com.nny.alarm.R;
import com.nny.alarm.edit.CycleEditor;
import com.uxwine.alarm.Alarm;
import com.uxwine.alarm.AlarmCalc;
import com.uxwine.alarm.TimeCycle2;
import com.uxwine.alarm.TimeRecord;
import com.uxwine.alarm.alert.AlarmContext;

public class AlarmBriefUtil
{
	Context mCtx;
	String[] mCycleStrings;
	
	static AlarmBriefUtil mInstance;
	public static synchronized AlarmBriefUtil getInstance(Context ctx)
	{
		if(null == mInstance)
			mInstance = new AlarmBriefUtil(ctx.getApplicationContext());
		return mInstance;
	}
	
	private AlarmBriefUtil(Context ctx)
	{
		mCtx = ctx;
		mCycleStrings = ctx.getResources().getStringArray(R.array.alerm_cycle);
	}
	
	public String getCycleString(TimeCycle2 cycle)
	{
		if(cycle.getCycle() == TimeCycle2.TYPE_WEEK)
		{
			
			final String[] weekdays = new DateFormatSymbols().getShortWeekdays();
			String text = "";
			for(int i = 0; i < 7; i++)
			{
				if(cycle.isWeekSet(i))
					text += weekdays[i + 1] + " ";
			}
			return text;			
		}
		return mCycleStrings[CycleEditor.getCyclePosition(cycle)];
	}
	
	@SuppressLint("DefaultLocale") 
	public String getBaseTimeString(Alarm a, Calendar trigger)
	{
		if(a.cycle.getCycle() == TimeCycle2.TYPE_HOUR)
		{
			int hour = a.time.get(TimeRecord.HOUR);
			int min = a.time.get(TimeRecord.MINUTE);
			return String.format("%02d:%02d", hour, min);
		}
		return (String)DateFormat.format("kk:mm", trigger);
	}
	
	public String getShortTimeString(Calendar trigger)
	{
		return (String)DateFormat.format("kk:mm", trigger);
	}
	
	public String getTriggerDetail(TimeCycle2 tc, Calendar trigger)
	{
		return (String)DateFormat.format("yyyy/MM/dd kk:mm", trigger);
	}
	
	public String getCycleString(final Alarm alarm)
	{
		final TimeCycle2 tc = alarm.cycle;
		Calendar trigger = AlarmCalc.triggerTime(alarm.time, alarm.cycle);
		switch(tc.getCycle())
		{
		case TimeCycle2.TYPE_ONCE:
		{
			trigger.setTimeInMillis(alarm.trigger);
			return DateFormat.format(mCtx.getString(R.string.time_format_once), trigger).toString();
		}
		case TimeCycle2.TYPE_YEAR:
			return DateFormat.format(mCtx.getString(R.string.time_format_every_year), trigger).toString();
		case TimeCycle2.TYPE_MONTH:
			return DateFormat.format(mCtx.getString(R.string.time_format_every_month), trigger).toString();
		case TimeCycle2.TYPE_HOUR:
			return mCtx.getString(R.string.edit_dlg_cycle_hour_item, tc.getFlags());
		default:
			return getCycleString(tc);
		}
	}
	
	public String getTimeString(final Alarm alarm)
	{
		Calendar trigger = null;
		if(alarm.trigger == 0)
		{
			trigger = AlarmCalc.triggerTime(alarm.time, alarm.cycle);
		}
		else
		{
			trigger = Calendar.getInstance();
			trigger.setTimeInMillis(alarm.trigger);
		}
		return getTriggerDetail(alarm.cycle, trigger);
	}
	
	////
	public String getAlarmTitle(final Alarm alarm)
	{
		return getTimeString(alarm);
	}
	
	public String getAlarmBrief(final Alarm alarm)
	{
		return alarm.label + "(" + getCycleString(alarm) + ")";
	}
	
	////
	public String getAlertTitle(final Alarm alarm)
	{
		return mCtx.getString(R.string.notify_alert_title);
	}
	
	public String getAlertBrief(final Alarm alarm)
	{
		return alarm.label + "(" + getCycleString(alarm) + ")";
	}
	
	//
	public String getSnoozedTitle(final Alarm alarm)
	{
		return mCtx.getString(R.string.notify_snooze_title);
	}
	
	public String getSnoozedBrief(final Alarm alarm)
	{
		return alarm.label + "(" + getCycleString(alarm) + ")";
	}
	//
	public String getTimeoutTitle(final Alarm alarm)
	{
		return mCtx.getString(R.string.notify_done_title);
	}
	
	public String getTimeoutBrief(final Alarm alarm)
	{
		return alarm.label + "(" + getCycleString(alarm) + ")";
	}
	
}
